package my.sample.snmp.threads;

import java.io.IOException;

import org.snmp4j.CommunityTarget;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class Poller {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {

		TransportMapping transport = new DefaultUdpTransportMapping();
		Snmp snmp = new Snmp(transport);
		
		
		MessageDispatcherImpl dispatcher = new MessageDispatcherImpl();

		transport.listen();
		Address targetAddress = new UdpAddress("202.144.0.178/161");
		// setting up target
		CommunityTarget target = new CommunityTarget();
		target.setCommunity(new OctetString("public"));
		target.setAddress(targetAddress);
		target.setRetries(2);
		target.setTimeout(1500);
		target.setVersion(SnmpConstants.version1);
		// creating PDU
		PDU pdu = new PDU();
		pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 1 })));
		pdu.add(new VariableBinding(new OID(new int[] { 1, 3, 6, 1, 2, 1, 1, 2 })));
		pdu.setType(PDU.GETNEXT);
		// sending request
		ResponseListener listener = new ResponseListener() {
			public void onResponse(ResponseEvent event) {
				// Always cancel async request when response has been received
				// otherwise a memory leak is created! Not canceling a request
				// immediately can be useful when sending a request to a
				// broadcast
				// address.
				((Snmp) event.getSource()).cancel(event.getRequest(), this);
				System.out.println("Received response PDU is: " + event.getResponse());
			}
		};
		snmp.set(pdu, target, null, listener);
		
//		snmp.sendPDU(pdu, target, null, listener);

	}
	// private static org.apache.log4j.Logger logger =
	// org.apache.log4j.Logger.getLogger(Poller.class);
}
